<template>
  <div>
    <el-card>
      <div><strong style="font-size: 20px">审批流程配置</strong></div>

      <div style="margin-top: 20px">审批层级</div>
      <el-card>
        <div v-for="item in approval_stops" :key="item.id" style="margin: 10px">
          <el-card style="width: 300px; position: relative">
            {{ item.stepNumber }}_{{ item.name }}
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              class="btn"
              @click="delFlows(item.id)"
            ></el-button>
          </el-card>
        </div>
        <el-button type="primary" plain @click="openDig()"
          >添加审批步骤</el-button
        >
      </el-card>
    </el-card>
    <el-dialog
      title="审批步骤"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="approvalFlowsForm"
        :model="approvalFlowsForm"
        label-width="80px"
      >
        <el-form-item
          label="审批名称"
          :rules="[
            { required: true, message: '请输入审批名称', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="approvalFlowsForm.name"
            placeholder="请输入审批名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="approvalFlowsForm.description"
            type="textarea"
            :rows="4"
            maxlength="128"
            show-word-limit
            placeholder="请输入描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="审核角色">
          <el-select
            v-model="approvalFlowsForm.approverRoleId"
            placeholder="请选择审核角色"
          >
            <el-option
              v-for="item in RoleOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="addFlors()">确 定</el-button>
          <el-button @click="close()">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style>
.top_input {
  margin: 4px;
}

.btn {
  position: absolute;
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
}
</style>

<script>
import flowsApi from "@/api/flows";
import roleApi from "@/api/role";
export default {
  data() {
    return {
      approval_stops: [],

      dialogVisible: false,

      approvalFlowsForm: {},
      RoleOptions: [],
    };
  },

  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    close() {
      this.dialogVisible = false;
      this.approvalFlowsForm = {};
      this.$refs["approvalFlowsForm"].resetFields();
    },
    getApprovalFlows() {
      flowsApi.getApprovalFlowsList().then((res) => {
        this.approval_stops = res.data.sort(
          (a, b) => a.stepNumber - b.stepNumber
        );
      });
    },

    getRoleOptions() {
      roleApi.getRoleListByUserSelect().then((res) => {
        this.RoleOptions = res.data;
      });
    },

    openDig() {
      this.getRoleOptions();
      this.dialogVisible = true;
    },

    addFlors() {
      this.$refs["approvalFlowsForm"].validate((valid) => {
        if (valid) {
          flowsApi.addApprovalFlows(this.approvalFlowsForm).then((res) => {
            if (res.code == 20000) {
              this.$message.success("添加成功");

              this.getApprovalFlows();
              this.close();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          return false;
        }
      });
    },

    delFlows(id) {
      flowsApi.deleteById(id).then((res) => {
        if (res.code == 20000) {
          this.$message.success("删除成功");
          this.getApprovalFlows();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },

  created() {
    this.getApprovalFlows();
  },
};
</script>
